package menu.managedBean;

import accountManagement.entity.Employee;
import accountManagement.sessionbean.EmployeeManager;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@ViewScoped
public class MenuBean implements Serializable {

    private MenuModel model;
    @EJB
    EmployeeManager employee;

    public MenuBean() {
    }

    public MenuModel getModel() {
//            if(model == null)
//            {
        model = new DefaultMenuModel();

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Boolean isLogin = (Boolean) request.getSession().getAttribute("isLogin");


        if (isLogin == true) {
            Long loginId = (Long) request.getSession().getAttribute("userId");
            Employee user = employee.getEmployeeById(loginId);
            List<String> userType = new ArrayList<String>();

            for (int i=0;i<user.getRoles().size();i++){
                userType.add(user.getRoles().get(i).getRoleName());
            }

            //First submenu
            Submenu submenu = new Submenu();
            submenu.setLabel("My Admin");

            MenuItem item = new MenuItem();
            item.setValue("My Info");
            item.setUrl("/utility/ViewInfo.xhtml");
            item.setIcon("ui-icon ui-icon-person");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Search Employee");
            item.setUrl("/utility/Search.xhtml");
            item.setIcon("ui-icon ui-icon-search");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Write Message");
            item.setUrl("/messages/createInternalMessage.xhtml");
            item.setIcon("ui-icon ui-icon-pencil");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("System Message");
            item.setUrl("/messages/systemInbox.xhtml");
            item.setIcon("ui-icon ui-icon-mail-open");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Private Message");
            item.setUrl("/messages/privateInbox.xhtml");
            item.setIcon("ui-icon ui-icon-mail-closed");
            submenu.getChildren().add(item);

            model.addSubmenu(submenu);


            //Second submenu
            submenu = new Submenu();
            submenu.setLabel("My Workspace");
            
            item = new MenuItem();
            item.setValue("Home");
            item.setUrl("/");
            item.setIcon("ui-icon ui-icon-home");
            submenu.getChildren().add(item);

            if (userType.contains("Salesman")||userType.contains("Sales Manager")) {
                
                item = new MenuItem();
                item.setValue("Company Mgt");
                item.setUrl("/companyManagement/companyManagement.xhtml");
                item.setIcon("ui-icon ui-icon-flag");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales Inquiry");
                item.setUrl("/salesOrderProcessing/salesInquiry.xhtml");
                item.setIcon("ui-icon ui-icon-comment");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales Quotation");
                item.setUrl("/salesOrderProcessing/salesQuotation.xhtml");
                item.setIcon("ui-icon ui-icon-note");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Purchase Order");
                item.setUrl("/salesOrderProcessing/purchaseOrder.xhtml");
                item.setIcon("ui-icon ui-icon-cart");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales Order");
                item.setUrl("/salesOrderProcessing/salesOrder.xhtml");
                item.setIcon("ui-icon ui-icon-document");
                submenu.getChildren().add(item);

//                if (user.getName().equals("xx")) {
//                    item = new MenuItem();
//                    item.setValue("Sales Order Admin");
//                    item.setUrl("/salesOrderProcessing/salesOrder.xhtml");
//                    item.setIcon("ui-icon ui-icon-check");
//                    submenu.getChildren().add(item);
//                }

            }
            
            if (userType.contains("Admin")) {
                    
                item = new MenuItem();
                item.setValue("Account Mgt");
                item.setUrl("/accountManagement/employeeMgt.xhtml");
                item.setIcon("ui-icon ui-icon-contact");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Role Mgt");
                item.setUrl("/accountManagement/roleMgt.xhtml");
                item.setIcon("ui-icon ui-icon-star");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Functionality Control");
                item.setUrl("/accountManagement/functionMgt.xhtml");
                item.setIcon("ui-icon ui-icon-wrench");
                submenu.getChildren().add(item);
            }
            
            model.addSubmenu(submenu);
        }
        return model;
    }
    //}
}
